import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/bottomSheetPicker/show_bottom_sheet_picker.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SelectItem<T> {
  T value;
  String title;

  SelectItem({this.value, this.title});
}

class SelectFormField<T> extends StatefulWidget {
  final T value;
  final String title;
  final String hintText;
  final List<SelectItem<T>> items;
  final ValueChanged<T> onChanged;
  final bool isRequired;
  final bool enabled;
  final bool isHideIcon;
  final LoaderTask<List<SelectItem<T>>> loaderTask;

  const SelectFormField({
    Key key,
    @required this.title,
    @required this.items,
    this.value,
    this.hintText,
    this.onChanged,
    this.isRequired = false,
    this.enabled = true,
    this.loaderTask,
    this.isHideIcon = false,
  }) : super(key: key);

  @override
  _SelectFormFieldState<T> createState() => _SelectFormFieldState<T>();
}

class _SelectFormFieldState<T> extends State<SelectFormField<T>> {
  String get valueString {
    if (widget.value == null) return null;

    var current = widget.items.firstWhere(
      (item) => item.value == widget.value,
      orElse: () => null,
    );
    return current?.title;
  }

  @override
  Widget build(BuildContext context) {
    return CustomListTitle(
      title: Row(
        children: <Widget>[
          Text(
            widget.title,
            style: TextStyleConstant().normal_1_28,
          ),
          if (widget.isRequired)
            Text(
              '*',
              style: TextStyleConstant().origin_28,
            )
        ],
      ),
      trailing: Row(
        children: <Widget>[
          Text(
            '${widget.value == null ? widget.hintText ?? '' : valueString}',
            style: widget.value == null ? TextStyleConstant().normal_4_28 : TextStyleConstant().normal_1_28,
          ),
          if (!widget.isHideIcon)
            Container(
              margin: EdgeInsets.only(
                left: 8.w,
              ),
              child: BaseImage(
                'assets/image/form_select_icon.png',
                width: 22.w,
                height: 10.w,
              ),
            ),
        ],
      ),
      onTap: () async {
        if (!widget.enabled) return;

        List<SelectItem<T>> items = widget.items;

        if (widget.items == null || widget.items.length == 0 && widget.loaderTask != null) {
          var res = await widget.loaderTask();

          if (res.isError) {
            return;
          }

          items = res.asValue.value;
        }

        showBottomSheetPicker<T>(
          context: context,
          items: items,
          onConfirm: widget.onChanged,
          value: widget.value,
        );
      },
    );
  }
}
